package game;

/**
 *	@(#)TurnOrder.java
 *
 *	The <code>TurnOrder</code> class represents the turn order.
 *	Turn order is determined by the amount of manned cartshops and explorers.
 *
 *	@author Grzegorz Kobiela
 *	@version 1.0 2007/9/11
 */
public final class TurnOrder
extends GameObject
{
	/** The current turn order. */
	private java.util.ArrayList<Integer> order = new java.util.ArrayList<Integer>();

	/** The index of the active player. */
	private int current = 0;

	/** False if <code>current</code> exceeds its bounds. */
	private boolean valid = false;

	/**
	 *	Returns the position of the player with the given index.
	 *	@param index the index
	 *	@return the position
	 */
	public int getPosition(int index) {
		int n = order.size();
		for(int i = 0; i < n; i++) if(order.get(i) == index) return i;
		throw new IllegalArgumentException(index + " is not supported by TurnOrder.getPosition(int)!");
	}

	/**
	 *	Returns the index of the active player.
	 *	@return the index
	 */
	public int getCurrent() {
		return order.get(current);
	}

	/**
	 *	Determines the next player.
	 */
	public void next() {
		if(++current >= order.size()) {
			current -= order.size();
			valid = false;
		}
	}

	/**
	 *	Determines the previous player.
	 */
	public void prev() {
		if(--current < 0) {
			current += order.size();
			valid = false;
		}
	}

	/**
	 *	Resets the turn order.
	 */
	public void reset() {
		reset(0);
	}

	/**
	 *	Resets the turn order to the given value.
	 *	@param value the value
	 */
	public void reset(int value) {
		current = (order.size() + value) % order.size();
		valid = true;
	}

	/**
	 *	Returns true if the current turn order is valid.
	 *	@return true if the current turn order is valid
	 */
	public boolean isValid() {
		return valid;
	}

	/**
	 *	Sets the new turn order to the given value.
	 *	@param order the new turn order
	 */
	public void setOrder(java.util.ArrayList<Integer> order) {
		this.order.clear();
		this.order.addAll(order);
	}
}